home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archivers / XpkDisk / ripcord.c < prev    next >
C/C++ Source or Header  |  1996-09-26  |  4KB  |  184 lines

  1. /*-
  2.  * RIPCORD.C
  3.  *
  4.  * The xpkdisk.device code that takes care of disk space availability.
  5.  *
  6.  * $Id: ripcord.c,v 1.4 1995/04/08 20:23:48 Rhialto Exp $
  7.  * $Log: ripcord.c,v $
  8.  * Revision 1.4  1995/04/08  20:23:48  Rhialto
  9.  * Add/correct version strings.
  10.  *
  11.  * Revision 1.3  1995/04/02  14:58:51  Rhialto
  12.  * Don't call EasyRequest() on older Intuition versions.
  13.  * Abort writing ripcord file when the write fails.
  14.  *
  15.  * Revision 1.2  1993/12/29  17:27:46  Rhialto
  16.  * Creating the ripcord file was not done right.
  17.  *
  18.  * Revision 1.1  1993/11/08  13:29:16  Rhialto
  19.  * Initial revision
  20.  *
  21.  *
  22.  * This code is (C) Copyright 1993 by Olaf Seibert. All rights reserved.
  23.  * May not be used or copied without a licence.
  24. -*/
  25.  
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include "xpkdisk.h"
  29. #include <intuition/intuitionbase.h>
  30. #include <clib/intuition_protos.h>
  31.  
  32. extern struct DosLibrary   *DOSBase;
  33. extern struct IntuitionBase *IntuitionBase;
  34.  
  35. /*#undef DEBUG            */
  36. #if DEBUG
  37. #   include "syslog.h"
  38. #else
  39. #   define    debug(x)
  40. #endif
  41.  
  42. static const char rcsId[] = "$Id: ripcord.c,v 1.4 1995/04/08 20:23:48 Rhialto Exp $";
  43. static const char   Ripcord[] = XPKDISKDIR "Ripcord";
  44. static const char   XpkdiskRequest[] = "xpkdisk Request";
  45.  
  46. int
  47. RipTryAbort(UNIT *unit, int ripcord)
  48. {
  49.     static struct EasyStruct es = {
  50.     sizeof(es),
  51.     0,
  52.     XpkdiskRequest,
  53.     "Help!!!\n" XPKDISKDIR " medium is (nearly) full!\n%s",
  54.     NULL
  55.     };
  56.     char       *extra;
  57.     int         choice;
  58.  
  59.     if (ripcord) {
  60.     extra = "(but the ripcord is still there)";
  61.     es.es_GadgetFormat = "Rip Cord|Try Anyway|Abort";
  62.     } else {
  63.     extra = "(and no ripcord is present)";
  64.     es.es_GadgetFormat = "Try Anyway|Abort";
  65.     }
  66.  
  67.     if (IntuitionBase->LibNode.lib_Version < 37) {
  68.     choice = 1;
  69.     } else {
  70.     choice = EasyRequest(NULL, &es, NULL, extra);
  71.     }
  72.     if ((ripcord == 0) && (choice == 1))
  73.     choice = 2;
  74.     return choice;
  75. }
  76.  
  77. Prototype int MakeRipcord(UNIT *unit);
  78.  
  79. int
  80. MakeRipcord(UNIT *unit)
  81. {
  82.     void       *v;
  83.     BPTR        fh;
  84.     long        length;
  85.  
  86.     /*
  87.      * Reserve space for 2 tracks, and add 1 sector for the file header
  88.      * of the second track.
  89.      */
  90.     length = XD_BPS + (2 * unit->xu_TrackLen);
  91.     unit->xu_RipcordBlocks = length / XD_BPS;
  92.  
  93.     v = AllocMem(1024, MEMF_ANY | MEMF_CLEAR);
  94.     if (v) {
  95.     if (fh = Open(Ripcord, MODE_READWRITE)) {
  96.         int         i;
  97.  
  98.         Seek(fh, 0, OFFSET_END);
  99.         i = (length - Seek(fh, 0, OFFSET_CURRENT)) / 1024;
  100.         while (i > 0) {
  101.         if (Write(fh, v, 1024) <= 0)
  102.             break;
  103.         i--;
  104.         }
  105.         Close(fh);
  106.     }
  107.     FreeMem(v, 1024);
  108.     }
  109.     return v && fh;
  110. }
  111.  
  112. int
  113. DeleteRipcord(UNIT *unit)
  114. {
  115.     return DeleteFile(Ripcord);
  116. }
  117.  
  118. Prototype int CheckRipcord(UNIT *unit);
  119.  
  120. int
  121. CheckRipcord(UNIT *unit)
  122. {
  123.     __aligned struct InfoData infodata;
  124.     BPTR        fl;
  125.     BPTR        ripcord;
  126.     int         choice;
  127.     int         result = 1;
  128.  
  129.     ripcord = Lock(Ripcord, SHARED_LOCK);
  130.     UnLock(ripcord);
  131.     fl = Lock("", SHARED_LOCK);
  132.     if (Info(fl, &infodata)) {
  133.     if ((infodata.id_NumBlocks - infodata.id_NumBlocksUsed) <
  134.                         unit->xu_RipcordBlocks) {
  135.         /* Disk nearly full */
  136.         /*       1  2  0 */
  137.         choice = RipTryAbort(unit, ripcord);
  138.         switch (choice) {
  139.         case 0:    /* Abort */
  140.         result = 0;
  141.         break;
  142.         case 1:    /* Rip Cord */
  143.         DeleteRipcord(unit);
  144.         case 2:    /* Try Anyway */
  145.         result = 1;
  146.         break;
  147.         }
  148.     }
  149.     }
  150.     UnLock(fl);
  151.     return result;
  152. }
  153.  
  154. Prototype int FullRetry(UNIT *unit, char *file, int xpkerr, int ioerr, char *msg);
  155.  
  156. int
  157. FullRetry(UNIT *unit, char *file, int xpkerr, int ioerr, char *msg)
  158. {
  159.     static struct EasyStruct es = {
  160.     sizeof(es),
  161.     0,
  162.     XpkdiskRequest,
  163.     "Panic!!!\n"
  164.         "Writing " XPKDISKDIR "%s failed!\n"
  165.         "(%s)\n"
  166.         "XPK error: %ld, IoErr(): %ld.%s",
  167.     "Retry|Revert To Old|Abort"
  168.     };
  169.     int choice;
  170.     char *sugg = " Perhaps the disk is full?";
  171.  
  172.     if (xpkerr == -4 && ioerr == ERROR_DISK_FULL) {
  173.     sugg = "\nDidn't you see all those \"Disk Full\" requesters?";
  174.     }
  175.  
  176.     if (IntuitionBase->LibNode.lib_Version < 37) {
  177.     choice = 1;
  178.     } else {
  179.     choice = EasyRequest(NULL, &es, NULL, file, msg,
  180.                  (long)xpkerr, (long)ioerr, sugg);
  181.     }
  182.     return choice;
  183. }
  184.